/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017, 2024 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "flash_def.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/
/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */

 
uint8_t dataBuffer[2048] = {0}; /*一个page 2112个字节，前2048个是数据*/
SDK_ALIGN(static uint8_t s_nand_program_buffer[4096], 4);

extern status_t flexspi_nor_flash_read(FLEXSPI_Type *base, uint32_t dstAddr, const uint32_t *src, uint32_t length);
extern unsigned short _acblack_cat[];
#define TEST_BUFFER_IN_FLASH _acblack_cat

void show_page_data(void)
{
    for(uint32_t i = 0;i<2048;i++)
    {
        if((i%16) == 0)
        {
            PRINTF("\r\n");
            PRINTF("i = %d ->",i);
        }
        PRINTF("%x ",dataBuffer[i]);
    }
}

int main(void)
{
    char ch;
    uint8_t regStatus = 0;
    status_t status;
    
    uint32_t block = 0;
    uint32_t page  = 0;
    
    /* Init board hardware. */
    BOARD_ConfigMPU();
    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();

    /* Just enable the trace clock, leave coresight initialization to IDE debugger */
    SystemCoreClockUpdate();
    CLOCK_EnableClock(kCLOCK_Trace);

    flexspi_flash_port_init(EXAMPLE_FLEXSPI,NAND_FLASH_PORT);
    status = flexspi_nand_get_status(EXAMPLE_FLEXSPI, 0xC0, &regStatus);
    
    status = flexspi_nand_get_status(EXAMPLE_FLEXSPI, 0xA0, &regStatus);
    status = flexspi_nand_set_status(EXAMPLE_FLEXSPI, 0xA0, 0);
    status = flexspi_nand_get_status(EXAMPLE_FLEXSPI, 0xA0, &regStatus);
    
    while(1){
        PRINTF("please type the block number\r\n");
        SCANF("%d,",&block);
        PRINTF("block number is %d\r\n",block);
        PRINTF("please type the page number\r\n");/* 纯数字，按回车键确认 */
        SCANF("%d,",&page);
        PRINTF("page number is %d\r\n",page);
        flexspi_flash_port_init(EXAMPLE_FLEXSPI,NAND_FLASH_PORT);
        status = flexspi_nand_page_data_read(EXAMPLE_FLEXSPI, (block<<6)+page, dataBuffer);   /* read in buffer */
        
        PRINTF("block %d,page % data :\r\n",block,page);
        show_page_data();
        PRINTF("\r\n");
    }
}
